#include "tl_client.h"

Tl_client::Tl_client(QWidget *parent)
    : QMainWindow(parent),
    login_window(new client_login(client)),
    client(new socket)
{
    QLibrary("socket.dll");
    ui.setupUi(this);
    connect(login_window, &client_login::startup, this, &Tl_client::show_window);
    connect(client, &socket::receive_message, this, &Tl_client::receive);
}

void Tl_client::send_message() {
    if (socket::critical(this, ui.textEdit->toPlainText())) {
        return;
    }
    QString message(ui.textEdit->toPlainText());
    client->send_message(message);
    //receive(message);
    ui.textEdit->clear();
}

Tl_client::~Tl_client()
{
    client->close();
    login_window->close();
}

void Tl_client::modify_username()
{
    QString username(ui.nameedit->text());
    if (socket::critical(this,username)) {
        return;
    }
    client->send_name(username);
    ui.nameedit->clear();
    ui.username->setText(username);
}

void Tl_client::show_window(QString name,QString IP) {
    ui.username->setText(name);
    ui.server_IP->setText(IP);
    show();
}

void Tl_client::receive(QString message) {
    ui.textBrowser->append(message);
}